﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    @*<style>
            .layui-table tbody .layui-table-cell {
                height: 60px;
            }
        </style>*@
    <script type="text/javascript">
        var renderTable;
        var setSelectData;
        var userType = 0;

        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'treetable', 'index', 'laydate', 'form', 'dropdown'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , element = layui.element
                , laydate = layui.laydate
                , dropdown = layui.dropdown;

            //日期范围
            laydate.render({
                elem: '#registrationTime'
                , range: true
            });
            $.getJSON("/Console/Roles/GetListJson?userType=1", function (result) {
                $('#state').append(new Option(top.lang.pleaseSelect, ""));
                $('#state').append(new Option(top.lang.enable, "1"));
                $('#state').append(new Option(top.lang.disable, "0"));
                $('#roles').append(new Option(top.lang.pleaseSelect, ""));
                let plat = result.data;
                plat.forEach(function (v) {
                    for (var key in top.lang) {
                        if (top.lang.hasOwnProperty(key)) {
                            if (key === v.remark) {
                                var text = top.lang[key];
                                $('#roles').append(new Option(text, v.id));
                            }
                        }
                    }
                });
                layui.form.render("select");
            });
            // 渲染表格
            renderTable = function () {
                table.render({
                    elem: '#table-datalist'
                    , url: '/Console/CRMAccount/GetListJson'
                    , toolbar: '#table-datalist-toolbar'
                    , limit: 20
                    , title: 'CRM账户'
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'index', title: '序号', type: 'numbers', width: 60, templet: '#zizeng' }
                        , {
                            field: 'fullName', title: '姓名', templet: function (res) {
                                return res.fullName;
                            }
                        }
                        , {
                            field: 'CRMAccount', title: 'CRM账户', templet: function (res) {
                                return res.loginName;
                            }
                        }
                        , {
                            field: 'MT5Account', title: 'MT5账号', templet: function (res) {
                                return res.realAccount;
                            }
                        }
                        , { field: 'createDate', title: '注册时间', width: 180 }
                        //, { field: 'email', title: '邮箱' }
                        //, { field: 'telephone', title: '手机号' }
                        , { field: 'crmUpEmail', title: '归属上级' }
                        , { field: 'mt5UpLogin', title: '上级MT5账户' }
                        , {
                            field: 'state', title: '状态', width: 80, templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '">'
                            }
                        }
                        , { field: 'operate', fixed: 'right', title: '操作', toolbar: '#table-datalist-bar', width: 150 }
                    ]]
                    , id: "tableReload"
                    , defaultToolbar: false
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');
                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                        top.SetSelectLanguage(layui);
                        layui.form.render("select");
                    }
                });
            };
            renderTable();

            //头工具栏事件
            table.on('toolbar(table-datalist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNewMenus", 2, '800px', '650px', '/Console/CRMAccount/Create');
                        break;
                    case 'batchSetRole':
                        var data = checkStatus.data;
                        if (data.length > 0) {
                            var ids = "";
                            for (var i = 0; i < data.length; i++) {
                                if (ids == "")
                                    ids = data[i].id;
                                else
                                    ids += "," + data[i].id;
                            }
                            if (ids != "") {
                                top.OpenPageByKey(layui, "batchSetRole", 2, '700px', '600px', '/Console/Roles/CheckedItems?ids=' + ids + '&userType=1');
                            }
                        } else {
                            top.AlertByKey(layui, "pleaseSelectAccount", 0);
                            return false;
                        }
                        break;
                    case "export":
                        $.getJSON("/Console/CRMAccount/GetListJson?page=0&limit=0", function (result) {
                            if (result.code == 0) {
                                table.exportFile('tableReload', result.data, 'xls');
                            }
                        });
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'view') {
                    parent.layui.index.openTabsPage("/Console/CRMAccount/GetCRMAccountInfo?id=" + data.id, data.fullName);
                    parent.layui.element.render('nav');
                }
                else if (obj.event === 'more') {
                    //下拉菜单
                    dropdown.render({
                        elem: this //触发事件的 DOM 对象
                        , show: true //外部事件触发即显示
                        , data: [{
                            title: top.lang.resetPassword
                            , id: 'resetPassword'
                        }, {
                            title: top.lang.resetFundPassword
                            , id: 'resetFundPassword'
                        }, {
                            title: top.lang.del
                            , id: 'del'
                        }]
                        , click: function (menudata) {
                            if (menudata.id === 'resetPassword') {
                                top.OpenPageByKey(layui, "resetPassword", 2, '700px', '500px', '/Console/CRMAccount/UpdatePwd?id=' + data.id + '&updateType=loginPwd');
                            } else if (menudata.id === 'resetFundPassword') {
                                top.OpenPageByKey(layui, "resetFundPassword", 2, '700px', '500px', '/Console/CRMAccount/UpdatePwd?id=' + data.id + '&updateType=tradePwd');
                            } else if (menudata.id === 'del') {
                                var postData = {
                                    id: data.id
                                };
                                top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/CRMAccount/Delete", "POST", "deleteSuccess", "deleteFail", function () {
                                    renderTable();
                                });
                            }
                        }
                        , align: 'right' //右对齐弹出（v2.6.8 新增）
                        , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                    })
                }
            });

            form.on('submit(slectCRMUp)', function (data) {
                top.OpenPageByKey(layui, "selectUpperLevel", 2, '1000px', '600px', '/Console/CRMAccount/SlectCRMUp');
            });

            form.on('submit(resetting)', function (data) {
                $("#divSearch input").each(function () {
                    $(this).val("");
                });
                $('#roles').val("");
                $('#state').val("");
                layui.form.render("select");
            });

            form.on('submit(search)', function (data) {
                //执行重载
                table.reload('tableReload', {
                    page: { curr: 1, layout: ['prev', 'page', 'next', 'refresh'] }
                    , where: {
                        account: $("#nameOrAccount").val(),
                        state: $("#state option:selected").val() == "" ? "-1" : $("#state option:selected").val(),
                        telephone: $("#telephone").val(),
                        email: $("#email").val(),
                        roles: ($("#roles option:selected").text() == "请选择" || $("#roles option:selected").text() == "Please select") ? "" : $("#roles option:selected").text(),
                        crmUp: $("#crmUpId").val(),
                        IdNo: $("#IDNO").val(),
                        registrationTime: $("#registrationTime").val()
                    }
                });
            });

            form.on('submit(refresh)', function (data) {
                //执行重载
                table.reload('tableReload', {
                    page: { curr: 1, layout: ['prev', 'page', 'next', 'refresh'] }
                    , where: {
                        account: $("#nameOrAccount").val(),
                        state: $("#state option:selected").val() == "" ? "-1" : $("#state option:selected").val(),
                        telephone: $("#telephone").val(),
                        email: $("#email").val(),
                        roles: ($("#roles option:selected").text() == "请选择" || $("#roles option:selected").text() == "Please select") ? "" : $("#roles option:selected").text(),
                        crmUp: $("#crmUpId").val(),
                        IdNo: $("#IDNO").val(),
                        registrationTime: $("#registrationTime").val()
                    }
                });
            });

            form.on('switch(table-state)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/CRMAccount/UpdateState",
                    data: {
                        id: this.value
                        , intState: state
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });
        });

    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card" lay-filter="component-form-element">
                <div class="layui-card-body layui-form layui-form-pane" style="padding: 15px;">
                    <div id="divSearch" class="layui-col-space10 layui-form-item">

                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="nameOrAccount">姓名/账户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" id="nameOrAccount" name="nameOrAccount" placeholder="nameOrAccount" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="email">邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" id="email" name="email" placeholder="email" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="state">状态</label>
                            <div class="layui-input-inline">
                                <select name="state" id="state">
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="registrationTime">注册时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="registrationTime" name="registrationTime" placeholder="registrationTime" lay-key="8">
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="telephone">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" id="telephone" name="telephone" placeholder="telephone" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="role">角色</label>
                            <div class="layui-input-inline">
                                <select name="roles" id="roles">
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="crmUp">上级</label>
                            <div class="layui-input-inline" style="width:150px;margin-right:1px">
                                <input type="text" id="crmUp" name="crmUp" placeholder="crmUp" class="layui-input">
                                <input id="crmUpId" name="crmUpId" type="hidden" />
                            </div>
                            <button id="slectCRMUp" name="slectCRMUp" lay-event="slectCRMUp" lay-submit lay-filter="slectCRMUp" class="layui-btn layui-btn-primary layui-border-blue" style="padding:0 5px;margin-right:10px">选择</button>
                        </div>
                        @*<div class="layui-col-md3">
                            <label class="layui-form-label" lay-event="IDNO">证件号码</label>
                            <div class="layui-input-inline">
                                <input type="text" id="IDNO" name="IDNO" placeholder="IDNO" class="layui-input">
                            </div>
                        </div>*@
                    </div>
                    <div class="layui-form-item" style="text-align:center;">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-event="search" lay-submit lay-filter="search">搜索</button>
                            <button class="layui-btn layui-btn-warm" lay-submit="" lay-filter="resetting" lay-event="resetting">重置</button>
                            <button class="layui-btn layui-btn-primary" lay-submit="" lay-filter="refresh" lay-event="refresh">刷新</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab-content">
                        <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                        <script type="text/html" id="zizeng">
                            {{d.LAY_TABLE_INDEX+1}}
                        </script>
                        <script type="text/html" id="table-datalist-toolbar">
                            <div class="layui-btn-container" id="layui-btn-container">
                                <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                                <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="export">导出</button>
                                <button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="batchSetRole">批量设置角色</button>
                                @*<button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="userStatistics">用户统计</button>*@
                            </div>
                        </script>
                        <script type="text/html" id="table-datalist-bar">
                            <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="view">查看</button>
                            <button class="layui-btn layui-btn-sm" lay-event="more">更多</button>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

